package com.sloth.service.impl;

import com.sloth.mapper.CartMapper;
import com.sloth.mapper.ImageMapper;
import com.sloth.mapper.ProductMapper;
import com.sloth.mapper.SeckillListMapper;
import com.sloth.pojo.*;
import com.sloth.service.ProductDetailsService;
import com.sloth.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Service
public class ProductDetailsServiceImpl implements ProductDetailsService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ImageMapper imageMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private SeckillListMapper seckillListMapper;


    // 根据 id 查多表
    @Override
    public ServerResponse getProductById(Integer proId) {

        Product product = productMapper.selectByPrimaryKey(proId);


        ProductExample productExample = new ProductExample();
        productExample.createCriteria().andProNameEqualTo(product.getProName());
        List<Product> productList = productMapper.selectByExample(productExample);


        ProductColorMemoryImageVO pcci = new ProductColorMemoryImageVO();

        List<String> color = new ArrayList<>();
        List<String> memory = new ArrayList<>();

        for (Product pro : productList) {
            if (!color.contains(pro.getProColor()))
                color.add(pro.getProColor());
            if (!memory.contains(pro.getProMemory()))
                memory.add(pro.getProMemory());
        }
        pcci.setProduct(product);
        pcci.setColor(color);
        pcci.setMemory(memory);


        ImageExample imageExample = new ImageExample();
        imageExample.createCriteria().andProIdEqualTo(proId);
        List<Image> images = imageMapper.selectByExample(imageExample);
        List<String> imagesList = new ArrayList<>();

        for (Image img : images) {
            imagesList.add(img.getImgPath());
        }
        pcci.setImages(imagesList);


        if (pcci != null) {
            return ServerResponse.getSuccess(pcci);
        }
        return ServerResponse.getFail(null);
    }


    // 多条件查询 id
    @Override
    public Integer getByConditions(String proName, String proColor, String proMemory) {

        ProductExample productExample = new ProductExample();

        ProductExample.Criteria productCriteria = productExample.createCriteria();


        if (proName != null) {
            productCriteria.andProNameEqualTo(proName);
        }
        if (proColor != null) {
            productCriteria.andProColorEqualTo(proColor);
        }
        if (proMemory != null) {
            productCriteria.andProMemoryEqualTo(proMemory);
        }

        List<Product> productList = productMapper.selectByExample(productExample);
        if (productList.size() > 0) {
            Product product = productList.get(0);
            return product.getProId();

        }
        return 0;
    }

    @Override
    public ServerResponse saveCart(Cart cart) {
        CartExample cartExample = new CartExample();
        cartExample.createCriteria().andProIdEqualTo(cart.getProId()).andCustIdEqualTo(cart.getCustId());
        List<Cart> cartList = cartMapper.selectByExample(cartExample);
        int rows = 0;
        cart.setProDate(new Date());
        cart.setState(0);

        if (cartList.size() == 1) {
            Cart oldCart = cartList.get(0);
            cart.setCartId(oldCart.getCartId());
            cart.setProCount(oldCart.getProCount()+cart.getProCount());
            rows = cartMapper.updateByPrimaryKey(cart);
        }else {
            rows = cartMapper.insert(cart);
        }


        if (rows > 0) return ServerResponse.saveSuccess(rows);
        else return ServerResponse.saveFail(rows);
    }

    @Override
    public ServerResponse saveBlob(Integer proId, String editorData) {
        Product product=productMapper.selectByPrimaryKey(proId);


        byte[] buffer = editorData.getBytes();


        if (product!=null){
            product.setProIntroduction(buffer);
            int rows=productMapper.updateByPrimaryKeyWithBLOBs(product);
            System.out.println("rows:"+rows);
            return ServerResponse.updateSuccess(rows);
        }
        return ServerResponse.updateFail(null);
    }

    @Override
    public ServerResponse getProduct(Integer proId) {
        Product product = productMapper.selectByPrimaryKey(proId);

        byte[] bytes = product.getProIntroduction();
        String str = new String(bytes);

        return ServerResponse.getSuccess(str);
    }


    @Override
    public ServerResponse getSeckillProduct(Integer proId) {
        Date date = new Date();

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,1);
        Date date2  = calendar.getTime();

        SeckillListExample seckillListExample = new SeckillListExample();
        seckillListExample.createCriteria().andProIdEqualTo(proId).andSecStartTimeLessThan(date).andSecEncTimeGreaterThan(date);
        List<SeckillList> seckillLists = seckillListMapper.selectByExample(seckillListExample);

        System.out.println("秒杀list 数量 : "+seckillLists.size());
        if (seckillLists.size()>0){
            SeckillList seckill = seckillLists.get(0);
            return ServerResponse.getSuccess(seckill);
        }else {

            SeckillListExample seckillListExample2 = new SeckillListExample();
            seckillListExample2.createCriteria().andProIdEqualTo(proId).andSecStartTimeGreaterThan(date).andSecStartTimeLessThan(date2);
            seckillLists = seckillListMapper.selectByExample(seckillListExample2);

            System.out.println("即将秒杀list 数量 : "+seckillLists.size());
            if (seckillLists.size()>0){
                SeckillList seckill = seckillLists.get(0);
                return ServerResponse.getSuccess(seckill);
            }
        }

        return null;
    }


    // 添加购物车


}
